package per.wsj.room.data

import androidx.annotation.WorkerThread
import per.wsj.room.BaseApp
import per.wsj.room.data.dao.UserDao
import per.wsj.room.data.entity.User

class UserRepository {

    private val userDao:UserDao = AppDatabase.getInstance(BaseApp.getContext()).userDao()

    fun getUsers() = userDao.getUsers()

    @WorkerThread
    suspend fun addUser(user:User){
        userDao.insertUser(user)
    }

    fun addUser2(user:User){
        Thread(Runnable {
            userDao.insertUser(user)
        }).start()

    }

    companion object{
        // For Singleton instantiation
        @Volatile private var instance: UserRepository? = null

        fun getInstance() =
            instance ?: synchronized(this) {
                instance ?: UserRepository().also { instance = it }
            }
    }
}